package com.situ.layuimini.config;

import com.situ.layuimini.interceptor.CORSInterceptor;
import com.situ.layuimini.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class MyWebMVCConfig implements WebMvcConfigurer {

    //webmvc中配置　跨域资源共享　　开启跨域
//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")
//                .allowedOriginPatterns("*")
//                .allowCredentials(true)
//                .allowedMethods("*")
//                .allowedHeaders("*");
//    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new CORSInterceptor()).addPathPatterns("/**");
        List<String> excludePatterns = new ArrayList<>();

        //登录请求可以不拦截
        excludePatterns.add("/login");
        //swagger相关的请求不拦截
        excludePatterns.add("/swagger-ui.html");
        excludePatterns.add("/webjars/**");
        excludePatterns.add("/v2/**");
        excludePatterns.add("/swagger-resources/**");

        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns(excludePatterns);
    }
}
